package com.reebake.ideal.security.config;

import com.reebake.ideal.security.code.CodeAuthenticationProvider;
import com.reebake.ideal.security.dao.UsernamePasswordAuthenticationProvider;
import com.reebake.ideal.security.handler.UserDetailsServiceImpl;
import com.reebake.ideal.security.qrcode.QrcodeAuthenticationProvider;
import com.reebake.ideal.security.service.UserSecurityService;
import com.reebake.ideal.verify.code.core.VerifyCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 单独配置，避免循环依赖
 */
@Configuration
@RequiredArgsConstructor
public class AuthenticationServerProviderConfig {

    private final UserSecurityService userSecurityService;

    @Bean
    AuthenticationProvider usernamePasswordAuthenticationProvider(PasswordEncoder passwordEncoder) {
    	UsernamePasswordAuthenticationProvider daoAuthenticationProvider = new UsernamePasswordAuthenticationProvider();
    	daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
        daoAuthenticationProvider.setUserDetailsService(new UserDetailsServiceImpl(userSecurityService));
    	daoAuthenticationProvider.setHideUserNotFoundExceptions(true);
    	
    	return daoAuthenticationProvider;
    }
    
    @Bean
    AuthenticationProvider codeAuthenticationProvider(UserSecurityService userSecurityService, VerifyCodeService verifyCodeService) {
        return new CodeAuthenticationProvider(userSecurityService, verifyCodeService);
    }

    @Bean
    AuthenticationProvider qrcodeAuthenticationProvider(UserSecurityService userSecurityService, VerifyCodeService verifyCodeService) {
        return new QrcodeAuthenticationProvider(userSecurityService, verifyCodeService);
    }

}
